package com.atguigu.web;

import com.atguigu.pojo.Book;
import com.atguigu.pojo.Cart;
import com.atguigu.pojo.CartItem;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CartServlet extends BaseServlet{
    private BookService bookService = new BookServiceImpl();

    /**
     * 加入购物车
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*System.out.println("加入购物车");
        System.out.println("商品编号为:"+req.getParameter("id"));*/

        //获取商品编号
        Integer id = WebUtils.parseInt(req.getParameter("id"), 0);
        //通过商品编号查询图书信息
        Book book = bookService.queryBookById(id);
        //将图书信息转换为购物车商品项
        CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice(), book.getPrice());
        //调用addItem()方法添加商品项
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if(cart == null){
            cart = new Cart();
            req.getSession().setAttribute("cart", cart);

        }
        cart.addItem(cartItem);

        req.getSession().setAttribute("lastName", cartItem.getName());

        //重定向回首页
        //resp.sendRedirect(req.getContextPath());

        //重定向回原商品所在的地址页面
        resp.sendRedirect(req.getHeader("Referer"));

    }

    /**
     * 清空购物车
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if(cart !=null){
            cart.clear();
        }


        resp.sendRedirect(req.getHeader("Referer"));


    }

    /**
     * 删除商品项
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取商品编号
        Integer id = WebUtils.parseInt(req.getParameter("id"), 0);
        //获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");

        if(cart !=null ){
            //删除购物车商品项
            cart.deleteItem(id);
            //重定向回到cart.jsp
            resp.sendRedirect(req.getHeader("Referer"));
        }






    }

    /**
     * 修改商品数量
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取商品编号,商品数量
        Integer id = WebUtils.parseInt(req.getParameter("id"), 0);
        Integer count = WebUtils.parseInt(req.getParameter("count"), 1);
        //获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");

        if(cart !=null ){
            //更改指定商品的数量
            cart.updateCount(id,count);
            //重定向回到cart.jsp
            resp.sendRedirect(req.getHeader("Referer"));
        }






    }


}
